package com.github.reweber.androidasyncsocketexamples.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.reweber.androidasyncsocketexamples.R;
import com.github.reweber.androidasyncsocketexamples.Z_UserInfoDao;
import com.github.reweber.androidasyncsocketexamples.bean.XXXX;
import com.github.reweber.androidasyncsocketexamples.tcp.Server;
import com.github.reweber.androidasyncsocketexamples.ui.WifiUtils;
import com.github.reweber.androidasyncsocketexamples.utils.JSONUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wanbo on 2017/1/18.
 */

public class VideoRecorderActivity extends AppCompatActivity {


    @BindView(R.id.textView8)
    View textView8;
    @BindView(R.id.layout_zuo)
    RelativeLayout layoutZuo;
    @BindView(R.id.textView2)
    TextView textView2;
    @BindView(R.id.layout_zhong)
    RelativeLayout layoutZhong;
    @BindView(R.id.textView6)
    View textView6;
    @BindView(R.id.layout_you)
    RelativeLayout layoutYou;
    @BindView(R.id.layout_szmx)
    LinearLayout layoutSzmx;
    @BindView(R.id.imageView12)
    ImageView imageView12;
    @BindView(R.id.wd)
    TextView wd;
    @BindView(R.id.tv_wd)
    TextView tvWd;
    @BindView(R.id.imcyzgz)
    ImageView imcyzgz;
    @BindView(R.id.sd)
    TextView sd;
    @BindView(R.id.tv_sd)
    TextView tvSd;
    @BindView(R.id.imfwjdk)
    ImageView imfwjdk;


    @BindView(R.id.imfwjdk2)
    ImageView imfwjdk2;
    @BindView(R.id.yw)
    TextView yw;
    @BindView(R.id.tv_yw)
    TextView tvYw;

    @BindView(R.id.textView3)
    TextView textView3;
    @BindView(R.id.wddd)
    TextView wddd;
    @BindView(R.id.ed_sd)
    EditText edSd;
    @BindView(R.id.gz)
    TextView gz;
    @BindView(R.id.tv_gz)
    TextView tvGz;
    @BindView(R.id.imfwjdk1)
    ImageView imfwjdk1;
    @BindView(R.id.gz1)
    TextView gz1;
    @BindView(R.id.tv_jq)
    TextView tvJq;
    @BindView(R.id.tvip)
    TextView tvip;
    @BindView(R.id.chaxun)
    Button chaxun;

    private Button btn;
    //    boolean ret = false;
    String wjm = "未命名";

    private Handler h = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {


                case 3:
                    Toast.makeText(VideoRecorderActivity.this, "断开连接！", Toast.LENGTH_SHORT).show();
                    Log.e("888", "断开连接！");
                    textView3.setText("未连接");

                    break;
                case 4:

                    Log.e("888", "服务器连接成功！");
                    Toast.makeText(VideoRecorderActivity.this, "服务器连接成功!", Toast.LENGTH_SHORT).show();
                    textView3.setText("已连接");
                    break;
                case 1:
                    wjm = (String) msg.obj;
                    Log.e("5555", "xx:" + wjm);
                    if (!TextUtils.isEmpty(wjm)) {
                        if (shedingwendu == "") {
                            Toast.makeText(VideoRecorderActivity.this, "请输入设定温度！", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        XXXX xxxx = JSONUtils.parseJSON(wjm, XXXX.class);
                        String w = xxxx.getW();
                        String s = xxxx.getS();
                        String g = xxxx.getG();
                        String j = xxxx.getJ();
                        String y = xxxx.getY();
                        z_userInfoDao.addUser(w, s, g, j, y);
                        tvSd.setText(s);
                        tvWd.setText(w);
                        tvGz.setText(g);
                        tvJq.setText(j);
                        tvYw.setText(y);


                    }


                    break;

                default:
                    break;
            }

        }
    };


    Server server;

    Z_UserInfoDao z_userInfoDao;
    public static String shedingwendu = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        z_userInfoDao = new Z_UserInfoDao(this);
        setContentView(R.layout.activity_video);
        Log.e("dddd", WifiUtils.getHostIP());
        ButterKnife.bind(this);
        server = new Server(WifiUtils.getHostIP(), 8080, h);

        TextView textView = (TextView) findViewById(R.id.tvip);

        EditText ed_sd = (EditText) findViewById(R.id.ed_sd);
        ed_sd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                shedingwendu = s.toString();
            }
        });

        Button chaxun = (Button) findViewById(R.id.chaxun);
        chaxun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(VideoRecorderActivity.this, JieMian.class));

            }
        });
        textView.setText(WifiUtils.getHostIP());


    }


    // 取消注册
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (server != null) {
            server = null;
        }
        System.exit(0);

    }


}
